package cn.software.controller;

import cn.software.anntation.LogAnnotation;

import cn.software.entity.param.ResourceParam;
import cn.software.entity.param.RoleResourceParam;
import cn.software.entity.vo.ResourceVo;
import cn.software.service.ResourceService;
import cn.software.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author gxy
 * @Date 2023/5/31 8:00
 * @PackageName:cn.software.comtroller
 * @ClassName: ResourceController
 */
@RequestMapping("/resource")
@Api(tags = "资源接口")
@RestController
public class ResourceController {

    @Autowired
    private ResourceService resourceService;


    @PostMapping("/addResource")
    @ApiOperation("增加资源")
    @LogAnnotation()
    public Result addResource(@RequestBody ResourceParam resourceParam) {
        boolean isSuccess = resourceService.addResource(resourceParam);
        if (isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("添加失败");
        }
    }

    @PostMapping("/updateResource")
    @ApiOperation("更新资源")
    public Result updateResource(@RequestBody ResourceParam resourceParam){
        boolean isSuccess = resourceService.updateResource(resourceParam);
        if (isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("更新失败");
        }
    }

    @PostMapping("/disableResource")
    @ApiOperation("禁用资源")
    public Result disableResource(@RequestBody ResourceParam resourceParam){
        boolean isSuccess = resourceService.disableResource(resourceParam);
        if (isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("禁用失败");
        }
    }

    @PostMapping("/enableResource")
    @ApiOperation("启用资源")
    public Result enableResource(@RequestBody ResourceParam resourceParam){
        boolean isSuccess = resourceService.enableResource(resourceParam);
        if (isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("启用失败");
        }
    }

    @PostMapping("/grantPermissions")
    @ApiOperation("赋予权限")
    public Result grantPermissions(@RequestBody RoleResourceParam roleResourceParam){
        boolean isSuccess = resourceService.grantPermissions(roleResourceParam);
        if (isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("赋予失败");
        }
    }

    @PostMapping("/deletePermissions")
    @ApiOperation("移除权限")
    public Result deletePermissions(@RequestBody RoleResourceParam roleResourceParam){
        boolean isSuccess = resourceService.deletePermissions(roleResourceParam);
        if (isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("移除失败");
        }
    }

    @GetMapping("/getAllSource")
    @ApiOperation("查询资源")
    public Result getAllSource(){
        List<ResourceVo> listResourceVo = new ArrayList<>();
        listResourceVo = resourceService.getAllSource();
        return Result.success(listResourceVo);
    }

    @GetMapping("/getSourceToRole")
    @ApiOperation("根据身份查询权限")
    public Result getSourceToRole(@RequestParam Integer id){
        List<ResourceVo> listResourceVo = resourceService.getSourceToRole(id);
        return Result.success(listResourceVo);
    }

    @PostMapping("/deleteResoure")
    @ApiOperation("删除资源")
    public Result deleteResoure(@RequestParam Integer id){
        boolean isSuccess = resourceService.deleteResource(id);
        if (isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("删除失败");
        }
    }
}
